package umage.figures;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D.Double;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;

import org.jhotdraw.draw.AttributeKeys;
import org.jhotdraw.draw.CreationTool;
import org.jhotdraw.draw.RectangleFigure;

@FigureInfo(name = "System Figure", icon = "SystemFigure.png", toolbar = "Figures", toolbarPriority = 0, tool = CreationTool.class)
public class SystemFigure extends RectangleFigure {
	
	public SystemFigure() {
		setAttribute(AttributeKeys.TEXT, "System Name");
	}
	
	@Override
	public Collection<Action> getActions(Double p) {
		ArrayList<Action> actions = new ArrayList<Action>();
		actions.add(new ChangeNameAction());
		return actions;
	}
	
	@Override
	protected void drawText(Graphics2D g) {
		String text = (String)getAttribute(AttributeKeys.TEXT);
		Rectangle2D.Double bounds = getBounds();
		double height = g.getFontMetrics().getStringBounds(text, g).getHeight();
		g.drawString(text, (int)(bounds.x + 2), (int)(bounds.y + height + 2));
	}
	
	protected class ChangeNameAction extends AbstractAction {
		
		public ChangeNameAction() {
			super("Change System Name...");
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String result = JOptionPane.showInputDialog(null, "New System Name:", getAttribute(AttributeKeys.TEXT));
			if (result == null || result.trim().length() == 0) return;
			setAttribute(AttributeKeys.TEXT, result.trim());
		}
	}

}
